package com.sduept.nwld.fault.handler;

import com.sduept.bigdata.fault.analysis.handler.impl.AbstractCommonHandler;
import com.sduept.bigdata.fault.common.FaultHandlerContext;
import com.sduept.bigdata.fault.common.HandlerContext;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.message.queue.DelayQueueMessage;
import com.sduept.message.queue.DelayQueueService;
import com.sduept.utils.SpringContext;

import java.util.Calendar;
import java.util.Date;

/**
 * 故障记录应填报资料检测<br/>
 *业务描述：根据新建故障内容，检测资料完整性 
 * @author xu
 */
public class FaultRecordDetectHandler extends AbstractCommonHandler {
	private ParamManager pcache = SpringContext.getBean(ParamManager.class);
	private DelayQueueService queueService = SpringContext.getBean(DelayQueueService.class);//队列消费者
	private static final String fault_detect_time_interval = "fault_detect_time_interval";	
	@Override
	public void logic(HandlerContext context) {
		FaultHandlerContext faultContext = (FaultHandlerContext)context;
		FaultRecord faultRecord = faultContext.getCurrentFaultRecord();
		if(faultRecord.getStatus()==1) {//1为非故障
			return;
		}
		String desc = getDescByFaultRecord(faultRecord);
		String faultId = faultRecord.getId();
		long currentTime = System.currentTimeMillis();
		SystemParameter param = pcache.get(fault_detect_time_interval);
		String[] timeInterval = param.getValue().split(",");
		long delayTime = Integer.valueOf(timeInterval[0])*1000;
		MessageTimeOutHandler handler = new MessageTimeOutHandler();
		DelayQueueMessage queueMessage = new DelayQueueMessage(faultId, currentTime, delayTime);
		queueMessage.setDelayQueueTimeoutHandlerName(handler.getClass().getName());
		queueMessage.setName(desc);
		queueService.offerDelayQueueAndCheckOnly(queueMessage);
	}
	
	
	
	/**
	 * 根据故障内容拼接短信标题
	 * @param faultRecord
	 * @return
	 */
	private String getDescByFaultRecord(FaultRecord faultRecord) {
        StringBuilder build = new StringBuilder();
        String time = transformDateToString(faultRecord.getFaultTime());
        build.append(time+","+faultRecord.getEquipment()
        +"发生"+faultRecord.getFaultPhase()+"相故障");
        if(faultRecord.getFaultType()!=null) {
        	build.append("，故障类型为："+faultRecord.getFaultType());
        }
		return build.toString();
	}
	
	/**
	 * 根据故障信息替换时间和设备名称
	 * @param date 
	 * @param fr
	 * @param messageTemplate 
	 */
	private String transformDateToString(Date date) {
		Calendar clDate =Calendar.getInstance();
		clDate.setTime(date);
		int year = clDate.get(Calendar.YEAR);
		int month = clDate.get(Calendar.MONTH)+1;
		int day = clDate.get(Calendar.DAY_OF_MONTH);
		int hour = clDate.get(Calendar.HOUR_OF_DAY);
		int minute = clDate.get(Calendar.MINUTE);
		int second = clDate.get(Calendar.SECOND);
		String time = year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒";
		return time;
	  }
	
	
	
	
	

}
